package com.procurement.inquiry.service.impl;

import java.util.List;

import com.procurement.inquiry.domain.PcmInquiryDetail;
import com.procurement.inquiry.domain.PcmSupplier;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.procurement.inquiry.domain.PcmApplicationDetail;
import com.procurement.inquiry.mapper.PcmInquiryDemandMapper;
import com.procurement.inquiry.domain.PcmInquiryDemand;
import com.procurement.inquiry.service.IPcmInquiryDemandService;

/**
 * 询报价需求Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class PcmInquiryDemandServiceImpl implements IPcmInquiryDemandService 
{
    @Autowired
    private PcmInquiryDemandMapper pcmInquiryDemandMapper;

    /**
     * 查询询报价需求
     * 
     * @param inquiryDemandNumber 询报价需求主键
     * @return 询报价需求
     */
    @Override
    public PcmInquiryDemand selectPcmInquiryDemandByInquiryDemandNumber(String inquiryDemandNumber)
    {
        return pcmInquiryDemandMapper.selectPcmInquiryDemandByInquiryDemandNumber(inquiryDemandNumber);
    }

    @Override
    public PcmSupplier selectSupplier(String expectedSupplier) {
        return pcmInquiryDemandMapper.selectSupplier(expectedSupplier);
    }

    /**
     * 查询询报价需求列表
     * 
     * @param pcmInquiryDemand 询报价需求
     * @return 询报价需求
     */
    @Override
    public List<PcmInquiryDemand> selectPcmInquiryDemandList(PcmInquiryDemand pcmInquiryDemand)
    {
        return pcmInquiryDemandMapper.selectPcmInquiryDemandList(pcmInquiryDemand);
    }

    @Override
    public List<PcmApplicationDetail> selectPcmApplicatioDetailList(PcmApplicationDetail pcmApplicationDetail) {
        return pcmInquiryDemandMapper.selectPcmApplicationDetailList(pcmApplicationDetail);
    }

    /**
     * 新增询报价需求
     * 
     * @param pcmInquiryDemand 询报价需求
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPcmInquiryDemand(PcmInquiryDemand pcmInquiryDemand)
    {
        int rows = pcmInquiryDemandMapper.insertPcmInquiryDemand(pcmInquiryDemand);
        //insertPcmApplicationDetail(pcmInquiryDemand);
        return rows;
    }

    /**
     * 修改询报价需求
     * 
     * @param pcmInquiryDemand 询报价需求
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePcmInquiryDemand(PcmInquiryDemand pcmInquiryDemand)
    {
        pcmInquiryDemandMapper.deletePcmApplicationDetailByInquiryDemandNumber(pcmInquiryDemand.getInquiryDemandNumber());
        insertPcmApplicationDetail(pcmInquiryDemand);
        return pcmInquiryDemandMapper.updatePcmInquiryDemand(pcmInquiryDemand);
    }

    /**
     * 批量删除询报价需求
     * 
     * @param inquiryDemandNumbers 需要删除的询报价需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmInquiryDemandByInquiryDemandNumbers(String[] inquiryDemandNumbers)
    {
        //pcmInquiryDemandMapper.deletePcmApplicationDetailByInquiryDemandNumbers(inquiryDemandNumbers);
        return pcmInquiryDemandMapper.deletePcmInquiryDemandByInquiryDemandNumbers(inquiryDemandNumbers);
    }

    @Override
    public int insertPcmInquiryDetail(PcmInquiryDetail pcmInquiryDetail) {
        return pcmInquiryDemandMapper.insertPcmInquiryDetail(pcmInquiryDetail);
    }

    /**
     * 删除询报价需求信息
     * 
     * @param inquiryDemandNumber 询报价需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmInquiryDemandByInquiryDemandNumber(String inquiryDemandNumber)
    {
        pcmInquiryDemandMapper.deletePcmApplicationDetailByInquiryDemandNumber(inquiryDemandNumber);
        return pcmInquiryDemandMapper.deletePcmInquiryDemandByInquiryDemandNumber(inquiryDemandNumber);
    }

    /**
     * 新增申请明细信息
     * 
     * @param pcmInquiryDemand 询报价需求对象
     */
    public void insertPcmApplicationDetail(PcmInquiryDemand pcmInquiryDemand)
    {
        List<PcmApplicationDetail> pcmApplicationDetailList = pcmInquiryDemand.getPcmApplicationDetailList();
        String inquiryDemandNumber = pcmInquiryDemand.getInquiryDemandNumber();
        if (StringUtils.isNotNull(pcmApplicationDetailList))
        {
            List<PcmApplicationDetail> list = new ArrayList<PcmApplicationDetail>();
            for (PcmApplicationDetail pcmApplicationDetail : pcmApplicationDetailList)
            {
                pcmApplicationDetail.setInquiryDemandNumber(inquiryDemandNumber);
                list.add(pcmApplicationDetail);
            }
            if (list.size() > 0)
            {
                pcmInquiryDemandMapper.batchPcmApplicationDetail(list);
            }
        }
    }
}
